#include<string.h>
#include<stdio.h>
#include<conio.h>

void RemoveSpace(char s[])
{
    int i,len = strlen(s);
    for (i = len-1; i >= 0; i-- )
    {
        if (s[i] == ' ')
        {
            int j;
            for (j=i; j<len; j++)
            {
                s[j] = s[j+1];
            }
            len--;
        }
    }
}

int GetNumber (char s[], int index, int length)
{
    int sLength = strlen(s);
    if(index<0 || index >= sLength || length<1)
    {
        printf("GetNumber error: index or length out of range!");
        return NULL;
    }
    if(index + length > sLength)
    {
        length = sLength - index;
    }

    int number = 0, i;
    for(i = index; i< index + length; i++)
    {
        if(s[i] >57 && s[i] < 48)
        {
            printf("GetNumber error: sub-string is not a number!");
            return NULL;
        }
        number = number * 10 + (int)s[i] - 48;
    }
    return number;
}

double calculate(int number1, int number2, char oper)
{
    switch(oper)
    {
    case '+' :
        return number1 + number2;
    case '-' :
        return number1 - number2;
    case '*' :
        return number1 * number2;
    case '/' :
        return (double)number1 / number2; //need to check 'division by zero'
    }
    return 0; //???
}

int ValidateData(char s[])
{
    //check data input right format of a + b
    return 1;
}

double GetResult(char s[])
{
    int i, len = strlen(s);
    for(i = 0; i< len; i++)
    {
        if (s[i] == '+' ||s[i] == '-' ||s[i] == '*' ||s[i] == '/')
        {
            return calculate( GetNumber(s,0, i),GetNumber(s,i+1,len-i-1),s[i]);
        }
    }
    return 0;//???
}

void Display()
{
    int num1, num2, lenS1, lenS2;
    char s[256],c,s1[256],s2[256];
    printf(" - Input an expression : ");
    gets(s);

    RemoveSpace(s);
    if(ValidateData(s))
    {
        printf("- Result: %s = %f", s, GetResult(s));
    }
    else
    {
        printf("Error: Input invalid format");
    }

}

void main()
{
    Display();
}
